﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WP7Client.Controller;
using System.Diagnostics;
using WP7Client.Managers;
using WP7Client.UserInterface.Pages;

namespace WP7Client.Helpers
{
    public static class ControllerHelper
    {
        public static ActivationController ActivationController;
        public static ChatController ChatController;
        public static ConversationsController ConversationsController;
        public static FriendController FriendController;
        public static SearchController SearchController;
        public static SearchResultController SearchResultController;
        public static InvitationController InvitationController;
        public static LoginController LoginController;
        public static ProfileController ProfileController;
        public static RegistrationController RegistrationController;
        public static ResendActivationController ResendActivationController;
        public static SettingController SettingController;
        public static SuggestionController SuggestionController;
        public static PeopleNearbyController PeopleNearbyController;
        public static OverviewController OverviewController;
        public static ProfileController MyProfileController;
        public static SocialNetworkAccountController SocialNetworkAccountController;
        public static WebBrowserController WebBrowserSocialNetworkController;
        public static MapController MapController;
        public static ResetPasswordController ResetPasswordController;
        public static ActivateNewPasswordController ActivateNewPasswordController;
        public static ActivateNewMobileNumberController ActivateNewMobileNumberController;
        public static ChangeMobileNumberController ChangeMobileNumberController;
        public static FriendNotificationController FriendNotificationController;

        private static Boolean initialized = false;

        /// <summary>
        /// Start het systeem op en zorgt dat de benodigde controllers aangemaakt worden (ControllerHelper.Initialize()).
        /// </summary>
        /// <param name="overviewPage">De huidige OverviewPage</param>
        public static void Startup(OverviewPage overviewPage)
        {
            if (!GlobalHelper.AppStarted)
            {
                ControllerHelper.Initialize();

                ControllerHelper.OverviewController.OverviewPage = overviewPage;

                ControllerHelper.MyProfileController = new ProfileController(overviewPage.PivotItemMyProfile);
                ControllerHelper.MyProfileController.LoadUser(UserHelper.Username);

                LocationManager.Start();
                if (NotificationManager.Current == null)
                    NotificationManager.Current = new NotificationManager();
                NotificationManager.Current.Connect();
                NotificationManager.Current.NotificationReceived += Current_NotificationReceived;

                RefreshManager.Start();
                SendMessageManager.Start();
                GlobalHelper.AppStarted = true;
            }
        }

        /// <summary>
        /// Sluit alle controllers af, stopt threads/timers.
        /// </summary>
        public static void Shutdown()
        {
            ControllerHelper.Initialized = false;

            LocationManager.Stop();
            NotificationManager.Current = null;

            RefreshManager.Stop();
            SendMessageManager.Stop();
            GlobalHelper.AppStarted = false;
        }

        /// <summary>
        /// Wordt aangeroepen vanuit ControllerHelper.Startup() en maakt de benodigde controllers aan.
        /// </summary>
        public static void Initialize()
        {
            if (!initialized)
            {
                ChatController = new ChatController();
                ConversationsController = new ConversationsController();
                FriendController = new FriendController();
                SearchController = new SearchController();
                SearchResultController = new SearchResultController();
                SettingController = new SettingController();
                PeopleNearbyController = new PeopleNearbyController();
                InvitationController = new InvitationController();
                OverviewController = new OverviewController();
                SocialNetworkAccountController = new SocialNetworkAccountController();
                WebBrowserSocialNetworkController = new WebBrowserController();
                MapController = new MapController();
                FriendNotificationController = new FriendNotificationController();
                SuggestionController = new SuggestionController();
                initialized = true;
            }
        }

        /// <summary>
        /// Roept Update(); aan op alle controllers.
        /// </summary>
        public static void Update()
        {
            bool Updating = false;

            Debug.WriteLine("ControllerHelper.Update()");

            if (Initialized && !Updating)
            {
                Updating = true;
                ConversationsController.Update();
                FriendController.Update();
                SearchController.Update();
                SearchResultController.Update();
                SettingController.Update();
                PeopleNearbyController.Update();
                InvitationController.Update();
                OverviewController.Update();
                SocialNetworkAccountController.Update();
                SuggestionController.Update();
                Updating = false;
            }
        }

        public static Boolean Initialized
        {
            get { return initialized; }
            set { initialized = value; }
        }

        /// <summary>
        /// Handelt een notificatie af.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">eventargs</param>
        static void Current_NotificationReceived(object sender, EventArgs e)
        {
            Debug.WriteLine("RECEIVED: " + (String)sender);
            if ((String)sender == "NewChatMessage")
                ControllerHelper.ConversationsController.NewIncomingMessages();
        }
    }
}
